Skip to content

Replace String.format with concatenation in CorrelationIdFormatter #46467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

Kguswo
Copy link
Contributor

@Kguswo Kguswo commented Jul 18, 2025

Replace String.format() with string concatenation for simple text joining in CorrelationIdFormatter.

Changes:

  • Use "[" + ... + "] " instead of String.format("[%s] ", ...) in constructor

Motivation:

  • Improves performance for simple string concatenation (no actual formatting needed)
  • String concatenation is more appropriate when no format specifiers are used
  • Reduces unnecessary overhead of format string parsing

This change maintains identical functionality while using a more efficient approach for basic string joining.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 18, 2025
@@ -76,7 +76,7 @@ public final class CorrelationIdFormatter {

private CorrelationIdFormatter(List<Part> parts) {
this.parts = parts;
this.blank = String.format("[%s] ", parts.stream().map(Part::blank).collect(Collectors.joining(" ")));
this.blank = "[" + parts.stream().map(Part::blank).collect(Collectors.joining(" ")) + "] ";
Copy link
Contributor

@kzander91 kzander91 Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we at this point just use the Collectors.joining() variant with a prefix and suffix?

Suggested change
this.blank = "[" + parts.stream().map(Part::blank).collect(Collectors.joining(" ")) + "] ";
this.blank = parts.stream().map(Part::blank).collect(Collectors.joining(" ", "[", "] "));

@philwebb
Copy link
Member

@OrangeDoro I'm sorry but we're a small team and we aren't able to respond to requests such as yours.

@philwebb
Copy link
Member

Thanks for the PR and the additional suggestion @kzander91. As this code is called pretty infrequently I don't think we have any performance issues. The other changes are pretty subjective and as such I'd prefer to leave things as they are.

@philwebb philwebb closed this Jul 21, 2025
@philwebb philwebb added the status: declined A suggestion or change that we don't feel we should currently apply label Jul 21, 2025
@wilkinsona wilkinsona removed the status: waiting-for-triage An issue we've not yet triaged label Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants